package com.asen.app.base

import android.os.Build
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.asen.libbase.databinding.ActivityBaseX5WebViewBinding
import com.asen.libcommon.base.iface.BaseJSCall
import com.asen.libcommon.base.viewbind.BaseViewBindFragment
import com.asen.libcommon.base.viewbind.viewBind
import com.asen.libcommon.widget.BaseX5WebView
import com.orhanobut.logger.Logger
import com.tencent.smtt.sdk.ValueCallback

/**
 * @date   : 2021/12/23
 * @author : asenLiang
 * @e-mail : 375427684@qq.com
 * @desc   :
 */
abstract class BaseX5WebFragment<T : BaseJSCall> : BaseViewBindFragment<ActivityBaseX5WebViewBinding>() {


    abstract fun onToolbarTitle(): String // 标题名字
    abstract fun isShowToolbar(): Boolean // 是否显示标题
    abstract fun webUrl(): String         // 地址
    abstract fun clearCache(): Boolean    // 是否刪除所有緩存


    override val mBinding: ActivityBaseX5WebViewBinding by viewBind()

    /** fragment关闭监听，关闭事件交给Activity */
    private var fragmentFinishCallBack: (() -> Unit)? = null

    override suspend fun initView(view: View) {
        super.initView(view)
        ARouter.getInstance().inject(this)
        if (isShowToolbar()) {
            setTitle(onToolbarTitle())
        } else {
            setToolbarGone()
        }
        lifecycleScope.launchWhenResumed {
            mBinding.mBaseTitle.setLeftClick { goBack() }
        }

        if (!TextUtils.isEmpty(webUrl())) mBinding.mBaseX5WebView.loadUrl(webUrl())

    }

    protected fun getWebView(): BaseX5WebView = mBinding.mBaseX5WebView

    /**
     * 设置 Title
     */
    fun setTitle(title: String) {
        mBinding.mBaseTitle.setTitle(title)
    }

    /**
     * 设置 toolbar 隐藏
     */
    fun setToolbarGone() {
        mBinding.mBaseTitle.visibility = View.GONE
    }

    /**
     * webView 返回上一层 或退出当前界面
     */
    private fun goBack(): Boolean {
        return mBinding.mBaseX5WebView.goBack(requireActivity(), call = {
            if (fragmentFinishCallBack == null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    requireActivity().finishAfterTransition()
                } else {
                    requireActivity().finish()
                }
            } else {
                fragmentFinishCallBack!!.invoke()
            }
        })
    }

    fun setFragmentFinishCallBack(callback: () -> Unit) {
        fragmentFinishCallBack = callback
    }

    override fun onDestroyView() {
        super.onDestroyView()
        try {
            mBinding.mBaseX5WebView.setCallBack(null, null)
            mBinding.mBaseX5WebView.destroy(clearCache())
        } catch (e: Exception) {
            Logger.d(e.message)
        }
    }

    /**
     * android触发js方法或往js添加新元素
     */
    protected fun addJsAction(actionName: String, callback: ValueCallback<String>?) {
        mBinding.mBaseX5WebView.addJsAction(actionName, callback)
    }

    protected fun loadUrl(url: String? = "") {
        mBinding.mBaseX5WebView.loadUrl(url)
    }

}